var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        uid:'',
        // 搜索类型 product => 商品,order=>订单
        currentType: 'product',
        product: {
            keywords: '',
            list: [],
            
        },
        orderlist:[],
        history: ['瓜','西瓜'],
        pageindex: 0,
        pagesize: 10,
    },
    created() {
        
    },
    methods: {
        // 搜索确认搜索
        onSearch:function(){
            var that = this
            if(that.currentType === 'product') that.pullProductList();
            if(that.currentType === 'order') that.pullOrderList()
        },
        // 快捷搜索
        handQuickSearch:function(e){
            var that = this
            that.product.keywords = e
            if(that.currentType=='product'){
                that.pullProductList()
            }else if(that.currentType=='order'){
                that.pullOrderList()
            }
            
        },
        // 清空历史
        handClearHistory:function(){
            var that = this
            api.setPrefs({
                key: that.uid,
                value:''
            })
        },
        // 获取商品搜索历史记录
        getSearchKeywordHistory4Prd: function () {
            var that = this
            // 获取用户ID
            api.getPrefs({
                key: 'uid'
            }, function (retuid, err) {
                if (retuid && retuid.value) {
                    const uid = retuid.value;
                    that.uid = uid;
                    if (uid != '') {
                        // 用户搜索历史
                        api.getPrefs({
                            key: uid
                        }, function (ret) {
                            if (ret && ret.value) {
                                that.history = ret.value.split(',')
                            }
                        })
                    }
                }
            });
        },
        // 删除某一个历史标签
        // handClearHistory(e){
        //     const history = this.product.history.filter(res=> res != e)
        //     api.setPrefs({
        //         key: this.uid,
        //         value:history.join(',')
        //     })
        //     this.product.history = history
        // },
        // 拉取商品列表
        pullProductList: function () {
            var that = this
            const keywords = that.product.keywords
            const params = {
                action: 'selectproductlist',
                Keywords: keywords,
                pageindex: that.pageindex,
                pagesize: that.pagesize,
            }
            if(keywords != ''){
                const history = that.history.concat(keywords)
                const newHistory = that.removeDuplicate(history)
                api.setPrefs({
                    key: that.uid,
                    value:newHistory.join(',')
                })
                that.history = newHistory
            }
            // that.sendPullProductList(params)
            ns.post(params, function (succeed, data, err) {
                if (succeed) {
                        that.product.list = data.productList;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });
        },
        pullOrderList:function(){
            var that = this
            const keywords = that.product.keywords
            const params = {
                action: 'getorderlist',
                keywords: keywords,
                startRowIndex: that.pageindex,
                pageSize: that.pagesize,
            }
            if(keywords != ''){
                const history = that.history.concat(keywords)
                const newHistory = that.removeDuplicate(history)
                api.setPrefs({
                    key: that.uid,
                    value:newHistory.join(',')
                })
                that.history = newHistory
            }
            // that.sendPullProductList(params)
            ns.post(params, function (succeed, data, err) {
                if(succeed){
                    that.orderlist = data.data.List;
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
                },
        // 数组去从
        removeDuplicate(arr){
            var that = this
            if (!Array.isArray(arr)) {
                console.log('type error!')
                return
            }
            var arrry= [];
             var  obj = {};
            for (var i = 0; i < arr.length; i++) {
                if (!obj[arr[i]]) {
                    arrry.push(arr[i])
                    obj[arr[i]] = 1
                } else {
                    obj[arr[i]]++
                }
            }
            console.log(arrry)
            return arrry;
        },
        // 获取商品列表
        sendPullProductList: function (params, add = false) {
            var that = this
            ns.post(params, function (succeed, data, err) {
                if (succeed) {
                    if (add) {
                        that.product.list = that.product.list.concat(data.productList)
                    } else {
                        that.product.list = data.productList;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });
        },
        //商品列表进入详情
        do_opendetail:function(ID){
            var that = this;
            ns.openWin('product_detail', '商品详情', {
                id:ID
            });
        },
        //订单进入详情
        open_order_detail:function(id){
            var that = this;
            ns.openWin('dl_order_detail', '订单详情', {
                id: id,
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.currentType = api.pageParam.currentType;//order=>订单
    app.getSearchKeywordHistory4Prd()
    // 获取搜索历史记录
    if (app.currentType === "product") {
        app.pullProductList()
    }else if(app.currentType === "order"){
        app.pullOrderList()
    }
};
